home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Classpath.java < prev    next >
Text File  |  1998-11-03  |  10KB  |  436 lines

  1. package com.symantec.itools.lang;
  2.  
  3.  
  4. import java.io.IOException;
  5. import java.io.File;
  6. import java.net.URL;
  7. import java.util.Enumeration;
  8. import java.util.Hashtable;
  9. import java.util.Vector;
  10. import com.symantec.itools.io.FileSystem;
  11. import com.symantec.itools.util.StringTokenizer;
  12.  
  13.  
  14. /**
  15.  * @author Symantec Internet Tools Division
  16.  * @version 1.0
  17.  * @since VCafe 3.0
  18.  */
  19.  
  20. public class Classpath
  21. {
  22.     /**
  23.      * @since VCafe 3.0
  24.      */
  25.     protected Hashtable        filesTables;
  26.  
  27.     /**
  28.      * @since VCafe 3.0
  29.      */
  30.     protected String[]         actualClasspath;
  31.  
  32.     /**
  33.      * @since VCafe 3.0
  34.      */
  35.     protected String[]         validClasspath;
  36.  
  37.     /**
  38.      * @since VCafe 3.0
  39.      */
  40.     protected ClasspathEntry[] validEntries;
  41.  
  42.     /**
  43.      * @since VCafe 3.0
  44.      */
  45.     protected Hashtable        actualEntriesTable;
  46.  
  47.     {
  48.         filesTables = new Hashtable();
  49.     }
  50.  
  51.     public Classpath()
  52.     {
  53.         this(System.getProperty("java.class.path"));
  54.     }
  55.  
  56.     public Classpath(String cp)
  57.     {
  58.         setClasspath(cp);
  59.     }
  60.  
  61.     /**
  62.      * @param cp TODO
  63.      * @since VCafe 3.0
  64.      */
  65.     public void setClasspath(String cp)
  66.     {
  67.         Vector   list;
  68.         String[] array;
  69.  
  70.         array           = new StringTokenizer(cp, File.pathSeparator).getTokens();
  71.         actualClasspath = new String[array.length];
  72.  
  73.         for(int i = 0; i < array.length; i++)
  74.         {
  75.             actualClasspath[i] = FileSystem.getCanonicalPath(array[i], true);
  76.         }
  77.  
  78.         array = FileSystem.getValidPath(cp, true);
  79.         list  = new Vector();
  80.  
  81.         for(int i = 0; i < array.length; i++)
  82.         {
  83.             if(!(list.contains(array[i])))
  84.             {
  85.                 list.addElement(array[i]);
  86.             }
  87.         }
  88.  
  89.         validClasspath = new String[list.size()];
  90.         list.copyInto(validClasspath);
  91.  
  92.         validEntries = new ClasspathEntry[validClasspath.length];
  93.  
  94.         for(int i = 0; i < validEntries.length; i++)
  95.         {
  96.             validEntries[i] = new ClasspathEntry(validClasspath[i]);
  97.         }
  98.  
  99.         actualEntriesTable = new Hashtable();
  100.  
  101.         for(int i = 0; i < actualClasspath.length; i++)
  102.         {
  103.             String type;
  104.  
  105.             if(new File(actualClasspath[i]).isFile())
  106.             {
  107.                 type = "ZIP";
  108.             }
  109.             else
  110.             {
  111.                 type = "FILE";
  112.             }
  113.  
  114.             actualEntriesTable.put(type + i, new ClasspathEntry(actualClasspath[i]));
  115.         }
  116.     }
  117.  
  118.     /**
  119.      * @since VCafe 3.0
  120.      */
  121.     public String[] getActualClasspath()
  122.     {
  123.         return (actualClasspath);
  124.     }
  125.  
  126.     /**
  127.      * @since VCafe 3.0
  128.      */
  129.     public String[] getValidClasspath()
  130.     {
  131.         return (validClasspath);
  132.     }
  133.  
  134.     /**
  135.      * @param url TODO
  136.      * @since VCafe 3.0
  137.      */
  138.     public ClasspathEntry getEntry(URL url)
  139.     {
  140.         String key;
  141.  
  142.         if(url == null)
  143.         {
  144.             return (null);
  145.         }
  146.  
  147.         key = url.getFile();
  148.  
  149.         if(!key.startsWith("/ZIP") && !key.startsWith("/FILE"))
  150.         {
  151.             return (null);
  152.         }
  153.  
  154.         return (getEntry(key.substring(1, key.indexOf('/', 1))));
  155.     }
  156.  
  157.     /**
  158.      * @param entry TODO
  159.      * @since VCafe 3.0
  160.      */
  161.     public boolean contains(String entry)
  162.     {
  163.         entry = FileSystem.getCanonicalPath(entry, true);
  164.  
  165.         for(int i = 0; i < actualClasspath.length; i++)
  166.         {
  167.             if(FileSystem.compareFilenames(actualClasspath[i], entry))
  168.             {
  169.                 return (true);
  170.             }
  171.         }
  172.  
  173.         return (false);
  174.     }
  175.  
  176.     /**
  177.      * @param entry TODO
  178.      * @since VCafe 3.0
  179.      */
  180.     public boolean isValid(String entry)
  181.     {
  182.         entry = FileSystem.getCanonicalPath(entry, true);
  183.  
  184.         for(int i = 0; i < validClasspath.length; i++)
  185.         {
  186.             if(FileSystem.compareFilenames(validClasspath[i], entry))
  187.             {
  188.                 return (true);
  189.             }
  190.         }
  191.  
  192.         return (false);
  193.     }
  194.  
  195.     /**
  196.      * @param key TODO
  197.      * @since VCafe 3.0
  198.      */
  199.     public ClasspathEntry getEntry(String key)
  200.     {
  201.         return ((ClasspathEntry)actualEntriesTable.get(key));
  202.     }
  203.  
  204.     /**
  205.      * @param file TODO
  206.      * @exception com.symantec.itools.lang.NotJavaNameException
  207.      * @since VCafe 3.0
  208.      */
  209.     public JavaName convertFileNameToJavaName(File file)
  210.         throws NotJavaNameException
  211.     {
  212.         return (convertFileNameToJavaName(FileSystem.getCanonicalPath(file, true)));
  213.     }
  214.  
  215.     /**
  216.      * @param fileName TODO
  217.      * @exception com.symantec.itools.lang.NotJavaNameException
  218.      * @since VCafe 3.0
  219.      */
  220.     public JavaName convertFileNameToJavaName(String fileName)
  221.         throws NotJavaNameException
  222.     {
  223.         for(int i = 0; i < validEntries.length; i++)
  224.         {
  225.             if(validEntries[i].isDirectory())
  226.             {
  227.                 String dir;
  228.  
  229.                 dir = validEntries[i].getName();
  230.  
  231.                 if(FileSystem.isInPath(fileName, dir))
  232.                 {
  233.                     return (new JavaName(fileName.substring(dir.length()).replace(File.separatorChar, '/')));
  234.                 }
  235.             }
  236.         }
  237.  
  238.         return (new JavaName(fileName.substring(fileName.lastIndexOf(File.separatorChar) + 1)));
  239.     }
  240.  
  241.     /**
  242.      * @since VCafe 3.0
  243.      */
  244.     public void load()
  245.     {
  246.         for(int i = 0; i < validEntries.length; i++)
  247.         {
  248.             if(filesTables.get(validEntries[i].getName()) == null)
  249.             {
  250.                 filesTables.put(validEntries[i].getName(), validEntries[i].load(this));
  251.             }
  252.         }
  253.     }
  254.  
  255.     /**
  256.      * @param entryName TODO
  257.      * @since VCafe 3.0
  258.      */
  259.     public ClasspathEntry find(String entryName)
  260.     {
  261.         for(int i = 0; i < validEntries.length; i++)
  262.         {
  263.             if(validEntries[i].find(entryName))
  264.             {
  265.                 return (validEntries[i]);
  266.             }
  267.         }
  268.  
  269.         return (null);
  270.     }
  271.  
  272.     /**
  273.      * @since VCafe 3.0
  274.      */
  275.     public String getActualClasspathAsString()
  276.     {
  277.         StringBuffer cp;
  278.         int          i;
  279.  
  280.         cp = new StringBuffer();
  281.  
  282.         for(i = 0; i < actualClasspath.length - 1; i++)
  283.         {
  284.             cp.append(actualClasspath[i]).append(File.pathSeparatorChar);
  285.         }
  286.  
  287.         cp.append(actualClasspath[i]);
  288.  
  289.         return (cp.toString());
  290.     }
  291.  
  292.     /**
  293.      * @since VCafe 3.0
  294.      */
  295.     public String getValidClasspathAsString()
  296.     {
  297.         StringBuffer cp;
  298.         int          i;
  299.  
  300.         cp = new StringBuffer();
  301.  
  302.         for(i = 0; i < validClasspath.length - 1; i++)
  303.         {
  304.             cp.append(validClasspath[i]).append(File.pathSeparatorChar);
  305.         }
  306.  
  307.         cp.append(validClasspath[i]);
  308.  
  309.         return (cp.toString());
  310.     }
  311.  
  312.     /**
  313.      * @param entry TODO
  314.      * @since VCafe 3.0
  315.      */
  316.     public Classpath removeEntry(String entry)
  317.     {
  318.         int index;
  319.  
  320.         entry = FileSystem.getCanonicalPath(entry, true);
  321.  
  322.         for(index = 0; index < validClasspath.length; index++)
  323.         {
  324.             if(FileSystem.compareFilenames(entry, validClasspath[index]))
  325.             {
  326.                 break;
  327.             }
  328.         }
  329.  
  330.         if(index <= validClasspath.length)
  331.         {
  332.             StringBuffer tempClasspath;
  333.  
  334.             tempClasspath = new StringBuffer();
  335.  
  336.             for(int i = 0; i < validClasspath.length; i++)
  337.             {
  338.                 if(i != index)
  339.                 {
  340.                     tempClasspath.append(validClasspath[i]).append(File.pathSeparatorChar);
  341.                 }
  342.             }
  343.  
  344.             return (new Classpath(tempClasspath.toString()));
  345.         }
  346.  
  347.         return (this);
  348.     }
  349.  
  350.     /**
  351.      * @param entry TODO
  352.      * @since VCafe 3.0
  353.      */
  354.     public Classpath prependEntry(String entry)
  355.     {
  356.         return (new Classpath(entry + File.pathSeparatorChar + getValidClasspathAsString()));
  357.     }
  358.  
  359.     /**
  360.      * @param entry TODO
  361.      * @since VCafe 3.0
  362.      */
  363.     public Classpath appendEntry(String entry)
  364.     {
  365.         return (new Classpath(getValidClasspathAsString() + File.pathSeparatorChar + entry));
  366.     }
  367.  
  368.     /**
  369.      * @since VCafe 3.0
  370.      */
  371.     public String[] getValidEntries()
  372.     {
  373.         String[] list;
  374.  
  375.         list = new String[validClasspath.length];
  376.  
  377.         for(int i = 0; i < list.length; i++)
  378.         {
  379.             list[i] = validClasspath[i];
  380.         }
  381.  
  382.         return (list);
  383.     }
  384.  
  385.     /**
  386.      * @since VCafe 3.0
  387.      */
  388.     public String[] getInvalidEntries()
  389.     {
  390.         Vector   tempList;
  391.         String[] list;
  392.  
  393.         tempList = new Vector();
  394.  
  395.         for(int i = 0; i < actualClasspath.length; i++)
  396.         {
  397.             boolean found;
  398.  
  399.             found = false;
  400.  
  401.             for(int j = 0; j < validClasspath.length; j++)
  402.             {
  403.                 if(validClasspath[j].equals(actualClasspath[i]))
  404.                 {
  405.                     found = true;
  406.                     break;
  407.                 }
  408.             }
  409.  
  410.             if(!(found))
  411.             {
  412.                 tempList.addElement(actualClasspath[i]);
  413.             }
  414.         }
  415.  
  416.         list = new String[tempList.size()];
  417.         tempList.copyInto(list);
  418.  
  419.         return (list);
  420.     }
  421.     
  422.     public void cleanup()
  423.     {        
  424.         for(int i = 0; i < validEntries.length; i++)
  425.         {
  426.             validEntries[i].cleanup();
  427.         }
  428.     }
  429.     
  430.     protected void finalize()
  431.         throws Throwable
  432.     {
  433.         super.finalize();        
  434.         cleanup();
  435.     }
  436. }